﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using ClassLibrary1;
using Domain.Entities;

namespace Webservice.Controllers
{
    public class HealthCareEntitiesController : ApiController
    {
        private MyHealthDbContext db = new MyHealthDbContext();

        // GET: api/HealthCareEntities
        public IQueryable<HealthCareEntity> GetHealthCareEntities()
        {
            return db.HealthCareEntities;
        }

        // GET: api/HealthCareEntities/5
        [ResponseType(typeof(HealthCareEntity))]
        public IHttpActionResult GetHealthCareEntity(int id)
        {
            var healthCareEntity = db.HealthCareEntities.Include(hce => hce.Type).SingleOrDefault(hce => hce.Id == id);
            if (healthCareEntity == null)
            {
                return NotFound();
            }

            return Ok(healthCareEntity);
        }

        // PUT: api/HealthCareEntities/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutHealthCareEntity(int id, HealthCareEntity healthCareEntity)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != healthCareEntity.Id)
            {
                return BadRequest();
            }

            db.Entry(healthCareEntity).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HealthCareEntityExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/HealthCareEntities
        [ResponseType(typeof(HealthCareEntity))]
        public IHttpActionResult PostHealthCareEntity(HealthCareEntity healthCareEntity)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.HealthCareEntities.Add(healthCareEntity);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = healthCareEntity.Id }, healthCareEntity);
        }

        // DELETE: api/HealthCareEntities/5
        [ResponseType(typeof(HealthCareEntity))]
        public IHttpActionResult DeleteHealthCareEntity(int id)
        {
            HealthCareEntity healthCareEntity = db.HealthCareEntities.Find(id);
            if (healthCareEntity == null)
            {
                return NotFound();
            }

            db.HealthCareEntities.Remove(healthCareEntity);
            db.SaveChanges();

            return Ok(healthCareEntity);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool HealthCareEntityExists(int id)
        {
            return db.HealthCareEntities.Count(e => e.Id == id) > 0;
        }
    }
}